<template>
	<view class="content">
		<view class="top">
			<view @click="search" class="top_info" style="border: 1px solid #FFF2CC;border-radius: 20rpx;">
				<u-icon name="search" color="#FFF2CC" size="28"></u-icon>
				<text>关键字</text>
			</view>
		</view>
		<u-sticky class="stick" offset-top="-1" bgColor="#283b65">
			<u-tabs @change="change" lineWidth="30" :activeStyle="activeStyle"
				:inactiveStyle="{color:'#FFF2CC','fontSize':'25rpx'}" :itemStyle="itemStyle" :list="list"></u-tabs>
		</u-sticky>
		<view class="box">
			<view v-for="item in infolist" :key="item._id">
				<post-item @isshow="shows" :item="item"></post-item>
			</view>
		</view>
		<u-loadmore :status="status ? 'loading':'nomore'" />
		<u-popup :closeOnClickOverlay="true" @close="cancel" :show="show" mode="bottom">
			<view class="feedback" style="height: 15vh;background-color: #3c3d3a;">
				<view class="info" @click="block">

					<u-icon name="minus-circle" color="#eee" size="20"></u-icon>
					<text style="font-size: 30rpx;">不感兴趣</text>
				</view>
				<view class="cancels" @click="cancel">
					取消
				</view>
			</view>
		</u-popup>
		<view class="work" @click="seachwore">
			<view class="content">
				找工作
			</view>
		</view>
	</view>
</template>

<script>
	import {
		store,
		mutations
	} from '@/uni_modules/uni-id-pages/common/store.js'
	// const onjs = uniCloud.importObject("objs")
	const db = uniCloud.database();
	const cmd = db.command
	import {
		filterblock
	} from "@/utils/filterblock.js"
	import {
		sortHot
	} from '@/utils/sorthotarr.js'
	export default {
		data() {
			return {
				keyword: '',
				list: [{
						name: '最新',
						current: 0
					},
					{
						name: "热门",
						current: 1
					},
				],
				itemStyle: {
					width: "130rpx",
					height: "80rpx",
					fontSize: "20rpx",
					// color:"#c2d5ff"
				},
				activeStyle: {
					fontSize: "30rpx",
					color: "#c2d5ff"
				},
				infolist: [],
				total: 0,
				show: false,
				//热门排序的视频
				hotvideo: [],
				//页面限制
				filter: 0,
				//加载状态
				status: true,
				e_id: 0,
				video_id: '',
				user_id: ''
			}
		},
		onLoad() {
			this.user_id = uniCloud.getCurrentUserInfo().uid
			this.getinfolist()
			console.log('触发1')
			uni.$on('updateindex',this.callback)
		},
		onPullDownRefresh() {
			console.log('拉了')
			this.status = true
			this.filter = 0
			this.infolist = []
			if (this.e_id == 0) {
				this.getinfolist()
			}
			if (this.e_id == 1) {
				this.getinfolisthot()
			}
		},
		onReachBottom() {
			if (!this.status) {
				return;
			}
			this.filter = this.filter + 8
			console.log('到底了')
			if (this.e_id == 0) {
				this.getinfolist()
			}
			if (this.e_id == 1) {
				this.getinfolisthot()
			}
		},
		methods: {
			
			//搜索
			search() {
				uni.navigateTo({
					url: "/pages_video/search/search"
				})
			},
			change(e) {
				if (e.current == 1) {
					this.filter = 0
					this.e_id = 1
					this.infolist = []
					this.status = true
					this.getinfolisthot()
				}
				if (e.current == 0) {
					this.filter = 0
					this.e_id = 0
					this.status = true
					this.infolist = []
					console.log('点没电')
					this.getinfolist()
				}
			},
			seachwore() {
				uni.navigateTo({
					url: '/pages-other/job/job'
				})
			},
			//无感屏蔽
			filterb() {
				this.infolist = this.infolist.filter(item => {
					return item._id != this.video_id
				})
			},
			//不感兴趣
			block() {
				if (!store.hasLogin) {
					uni.showToast({
						title: "未登录",
						icon: 'error'
					})
					return;
				}
				uni.showLoading({

				})
				db.collection('video-articles').where({
					_id: this.video_id
				}).get().then(res => {
					let temp = []
					temp.push(uniCloud.getCurrentUserInfo().uid)
					console.log(res, '我个人的')
					if (!res.result.data[0].report) {
						//不纯在
						db.collection('video-articles').where({
							_id: this.video_id
						}).update({
							report: temp
						}).then(res => {
							this.filterb()
							if (this.infolist.length <= 6) {
								this.status = false
							}
							uni.hideLoading()
							console.log(res, "陈公公666")
						})
						this.show = false
					} else {
						console.log(res.result.data[0].report.indexOf(uniCloud.getCurrentUserInfo().uid) == -1)
						if (res.result.data[0].report.indexOf(uniCloud.getCurrentUserInfo().uid) == -1) {
							let arr = res.result.data[0].report
							arr.push(uniCloud.getCurrentUserInfo().uid)
							db.collection('video-articles').where({
								_id: this.video_id
							}).update({
								report: arr
							}).then(res => {
								this.filterb()
								if (this.infolist.length <= 6) {
									this.status = false
								}
								uni.hideLoading()
								console.log(res, "陈公公2")
							})
							this.show = false
						} else {
							this.filterb()
							if (this.infolist.length <= 6) {
								this.status = false
							}
							uni.hideLoading()

							console.log('有了')
							this.show = false
						}

					}
				})
			},
			shows(e) {
				console.log(e, "2223")
				this.video_id = e
				this.show = true
			},
			cancel() {
				this.show = false
			},
			callback() {
				this.infolist=[]
				console.log("执行updateindex")
				this.status = true
				this.filter = 0

				this.getinfolist()

			},
			//获取视频列表
			async getinfolist() {

				let videoTemp = db.collection("video-articles").field(
					'user_id,view_count,publish_date,content,report,avatar').getTemp()
				let userTemp = db.collection('uni-id-users').field('_id,avatar,nickname').getTemp()
				await db.collection(videoTemp, userTemp).where(`report!='${this.user_id}'`).orderBy(
					'publish_date desc').skip(this.filter).limit(8).get().then(res => {
					uni.stopPullDownRefresh()
					let oldarr = this.infolist
					if (res.result.data.length == 0) {
						this.status = false
					}
					this.infolist = [...oldarr, ...res.result.data]
					console.log(this.infolist)
					if (this.infolist.length <= 6) {
						this.status = false
					}

				})
			},
			async getinfolisthot() {

				let videoTemp = db.collection("video-articles").field(
					'user_id,view_count,publish_date,content,report,avatar').getTemp()
				let userTemp = db.collection('uni-id-users').field('_id,avatar,nickname').getTemp()
				db.collection(videoTemp, userTemp).where(`report!='${uniCloud.getCurrentUserInfo().uid}'`).orderBy(
					'view_count desc').skip(this.filter).limit(8).get().then(
					async res => {
						uni.stopPullDownRefresh()
						let oldarr = this.infolist
						if (res.result.data.length == 0) {
							this.status = false
						}
						this.infolist = [...oldarr, ...res.result.data]
						this.infolist = filterblock(this.infolist, uniCloud.getCurrentUserInfo().uid)
						if (this.infolist.length <= 6) {
							this.status = false
						}
						console.log(this.infolist)


					})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		.work {
			position: fixed;
			top: 350rpx;
			height: 100rpx;
			width: 100rpx;
			background-color: #76D7C4;
			font-weight: 900;
			border-radius: 100rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			opacity: 0.8;

			.content {
				font-size: 30rpx;
				font-family: '楷体';
			}
		}

		.top {
			padding: 20rpx;

			.top_info {
				padding: 5rpx;
				color: #F2E8CF;
				display: flex;
				align-items: center;

				text {
					margin-left: 20rpx;
					font-size: 30rpx;
					font-family: '楷体';
				}
			}
		}


		.feedback {
			display: flex;
			flex-direction: column;
			justify-content: space-between;

			.info {

				display: flex;
				justify-content: center;
				color: #F2E8CF;
				align-items: center;
				margin-top: 20rpx;
				flex: 2;
			}

			.cancels {
				flex: 1;
				border-top: 1px solid #1f1f1f;
				text-align: center;
				font-size: 35rpx;
				color: #F2E8CF;
				font-family: '楷体';
				display: flex;
				flex-direction: column;
				justify-content: center;
			}
		}

		.box {
			padding: 10rpx;
			display: flex;
			justify-content: space-between;
			flex-wrap: wrap;
		}
	}
</style>
<style>
	page {
		background-color: #11265c;
	}
</style>